********************************************************************************
* Figure 2: Political involvement stratisfied by pre-involvement
********************************************************************************
use  "Data_temp/polit_involv.dta", clear

* Identify pre-eligibility political involvement and stratisfy
gen m1=index if inrange(age100,-24,24)
bys pidp: egen m2=mean(m1)
sum m2, d
gen index_above=(m2>r(p50)) if m2<.

* election dummies
tab year if index<. & inrange(age0,-24,24) & rel_year>=0, gen(dummy)
drop dummy1

* Split by index of political involvement (panel a)
qui foreach var in index  party_closer1 interest_polit vote_agree vote_party member_party { 

	rdrobust `var' age0 if age0!=0  & rel_year>=0 & index_above<., p(1) h(24)  covs(dummy*)		
		global eff_obs=e(N_h_l)+e(N_h_r)
		regsave  using "Data_temp/attitude_`var'_1.dta", replace ci addlabel(sample, $eff_obs)

	rdrobust `var' age0 if age0!=0  & rel_year>=0 & index_above==0, p(1) h(24)  covs(dummy*)
		regsave  using "Data_temp/attitude_`var'_2.dta", replace ci 

	rdrobust `var' age0 if age0!=0  & rel_year>=0 & index_above==1, p(1) h(24)  covs(dummy*)
		regsave  using "Data_temp/attitude_`var'_3.dta", replace ci 
		
		}

* Split by individual variable	
qui foreach var in party_closer1 interest_polit vote_agree vote_party  { 

	cap drop m? 
	gen m1=`var' if inrange(age100,-24,24)
	bys pidp: egen m2=mode(m1)
	gen `var'_above=(m2==1) if m2<.
	
	}
	
qui foreach var in index   party_closer1 interest_polit vote_agree vote_party  { 
	
	
	rdrobust `var' age0 if age0!=0  & rel_year>=0 & `var'_above<., p(1) h(24)  covs(dummy*)
		global eff_obs=e(N_h_l)+e(N_h_r)
		regsave  using "Data_temp/attitudex_`var'_1.dta", replace ci addlabel(sample, $eff_obs)

	rdrobust `var' age0 if age0!=0  & rel_year>=0 & `var'_above==0, p(1) h(24)  covs(dummy*)
		regsave  using "Data_temp/attitudex_`var'_2.dta", replace ci 

	rdrobust `var' age0 if age0!=0  & rel_year>=0 & `var'_above==1, p(1) h(24)  covs(dummy*)
		regsave  using "Data_temp/attitudex_`var'_3.dta", replace ci 
	
	}	

	
*******************************
* Panel a: by index
clear
gen spec=.
gen outcome=""
gen n=.
local m=1
foreach var in index   party_closer1 interest_polit vote_agree vote_party member_party { 
	forvalues x = 1/3 {
		append using "Data_temp/attitude_`var'_`x'.dta"
		replace spec=`x' if spec==.
		replace outcome="`var'" if outcome==""
		replace n=`m' if n==.
		}
		local ++m
	}
	
	
replace n=n-0.15 if spec==1
replace n=n+0.15 if spec==3
		
		

twoway (rcap ci_lo ci_up n, lcolor(gs8) lwidth(medthin)) ///
		(scatter coef n if spec==1,  msize(medlarge) msymbol(D) mfcolor(white) mlwidth(medthick) mlcolor(black)) ///
		(scatter coef n if spec==2, msize(medlarge) msymbol(O) mfcolor(white) mlwidth(medthick) mlcolor(navy)) ///
		(scatter coef n if spec==3, msize(medlarge) msymbol(T) mfcolor(white) mlwidth(medthick) mlcolor(edkblue))		, ///
		 xsc(r(0.7 6.3))  xt("")  yl(-.2(0.1)0.3) ///
		xlabel(none) legend(order(2 "Pooled" 3 "Below median" 4 "Above median") row(1) size(small) ///
		subtitle("Pre-eligibility political involvement", size(medsmall)) ring(0) bplacement(sw) ///
		region(lstyle(none))) ///
		subt("")   yt("RD estimate") ///
		 xlab(1 `""Index of" "political involvement""' 2 `""Supporting or close" "to one party""' ///
		 3 `""Interested in" "politics""' 4 `""Voting as a" "social norm""' 5 `""States a party" "to vote for""' 6 `""Member of a" "political party""', ///
		labsize(*0.9) ) graphregion(margin(zero)) ///
		yline(0, lwidth(thin) lcolor(gs12) lpattern(solid))
graph export "Figures/fg2_a.eps", replace	



*******************************
* Panel b: by outcome variables
drop if coef<.

local m=1
foreach var in   party_closer1 interest_polit vote_agree vote_party  { 
	forvalues x = 1/3 {
		append using "Data_temp/attitudex_`var'_`x'.dta"
		replace spec=`x' if spec==.
		replace outcome="`var'" if outcome==""
		replace n=`m' if n==.
		}
		local ++m
	}
	
	
replace n=n-0.15 if spec==1
replace n=n+0.15 if spec==3
		
		

twoway (rcap ci_lo ci_up n, lcolor(gs8) lwidth(medthin)) ///
		(scatter coef n if spec==1,  msize(medlarge) msymbol(D) mfcolor(white) mlwidth(medthick) mlcolor(black)) ///
		(scatter coef n if spec==2, msize(medlarge) msymbol(O) mfcolor(white) mlwidth(medthick) mlcolor(navy)) ///
		(scatter coef n if spec==3, msize(medlarge) msymbol(T) mfcolor(white) mlwidth(medthick) mlcolor(edkblue)), ///
		 xsc(r(0.7 4.3)) xt("")  yl(-.2(0.2)0.8) ///
		xlabel(none) legend(order(2 "Pooled" 3 "0" 4 "1") row(1) size(small) ///
		subtitle("Pre-eligibility value", size(medsmall)) ring(0) bplacement(sw) ///
		region(lstyle(none)) ) ///
		subt("")   ///
		 xlab(1 `""Supporting or close" "to one party""' ///
		 2 `""Interested in" "politics""' 3 `""Voting as a" "social norm""' 4 `""States a party" "to vote for""', ///
		labsize(*1) ) graphregion(margin(zero)) yt("RD estimate") ///
		yline(0, lwidth(thin) lcolor(gs12) lpattern(solid))
graph export "Figures/fg2_b.eps", replace	